﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web;

namespace SusuCMS.Web
{
    public static class HtmlHelperExtensions
    {
        #region LabelFor

        public static MvcHtmlString LabelFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, string>> expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            var name = memberExpression.Member.Name;

            foreach (var item in htmlHelper.ViewData.ModelMetadata.Properties)
            {
                if (item.PropertyName == name && item.IsRequired == true)
                {
                    return new MvcHtmlString(htmlHelper.LabelFor<TModel, string>(expression).ToString() + "<span class=\"required\">*</span>");
                }
            }

            return htmlHelper.LabelFor<TModel, string>(expression);
        }

        #endregion


        #region HelpTip

        public static MvcHtmlString HelpTip<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, string content)
        {
            var memberExpression = expression.Body as MemberExpression;

            var tagBuilder = new TagBuilder("span");
            tagBuilder.Attributes.Add("class", "help-tip");
            tagBuilder.Attributes.Add("data-for", memberExpression.Member.Name);
            tagBuilder.Attributes.Add("title", content);

            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString HelpTip(this HtmlHelper htmlHelper, string content, string text = "[?]")
        {
            var tagBuilder = new TagBuilder("a");
            tagBuilder.SetInnerText(text);
            tagBuilder.Attributes.Add("class", "help-tip");
            tagBuilder.Attributes.Add("href", "javascript://");
            tagBuilder.Attributes.Add("title", content);

            return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal));
        }

        #endregion


        #region CheckBoxList

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items)
        {
            return CheckBoxList(helper, name, items, null, null);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            return CheckBoxList(helper, name, items, null, checkboxHtmlAttributes);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues)
        {
            return CheckBoxList(helper, name, items, selectedValues, null);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IDictionary<string, string> items, IEnumerable<string> selectedValues, IDictionary<string, object> checkboxHtmlAttributes)
        {
            var selectListItems = from i in items
                                  select new SelectListItem
                                  {
                                      Text = i.Key,
                                      Value = i.Value,
                                      Selected = (selectedValues != null && selectedValues.Contains(i.Value))
                                  };

            return CheckBoxList(helper, name, selectListItems, checkboxHtmlAttributes);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items)
        {
            return CheckBoxList(helper, name, items, null);
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, IDictionary<string, object> checkboxHtmlAttributes)
        {
            var output = new StringBuilder();
            foreach (var item in items)
            {
                output.Append("<label>");
                var checkboxList = new TagBuilder("input");
                checkboxList.MergeAttribute("type", "checkbox");
                checkboxList.MergeAttribute("name", name);
                checkboxList.MergeAttribute("value", item.Value);

                // Check to see if it’s checked
                if (item.Selected)
                {
                    checkboxList.MergeAttribute("checked", "checked");
                }

                // Add any attributes
                if (checkboxHtmlAttributes != null)
                {
                    checkboxList.MergeAttributes(checkboxHtmlAttributes);
                }

                checkboxList.SetInnerText(item.Text);
                output.Append(checkboxList.ToString(TagRenderMode.SelfClosing));
                output.Append(item.Text + "</label>");
            }

            return MvcHtmlString.Create(output.ToString());
        }
        #endregion


        public static PageHtml PageHtml(this HtmlHelper htmlHelper)
        {
            return new PageHtml(htmlHelper);
        }

        public static PageContext PageContext(this HtmlHelper htmlHelper)
        {
            return CmsApplication.GePageContext();
        }
    }
}